package top.zhangjianyong.tools.bill.manager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import top.zhangjianyong.tools.bill.manager.analyzer.WaBillAnalyzer;
import top.zhangjianyong.tools.entity.WaBill;
import top.zhangjianyong.tools.vo.DailyBill;
import top.zhangjianyong.tools.vo.MonthBill;

import java.time.LocalDate;
import java.time.YearMonth;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author ： zhangjianyong
 * @date ： 2022/7/23 4:29 PM
 */
@Component
public class WaBillAnalyzerManager {
    @Autowired
    private ApplicationContext applicationContext;
    private Map<String, WaBillAnalyzer> WA_BILL_ANALYZER_MAP;

    private Map<String, WaBillAnalyzer> getWaBillAnalyzerMap() {
        if (WA_BILL_ANALYZER_MAP != null) return WA_BILL_ANALYZER_MAP;
        WA_BILL_ANALYZER_MAP = applicationContext
                .getBeansOfType(WaBillAnalyzer.class)
                .values()
                .stream()
                .collect(Collectors.toMap(WaBillAnalyzer::getPlatform, resolver -> resolver));
        return WA_BILL_ANALYZER_MAP;
    }

    /**
     * 获取对应的分析器
     *
     * @param platform
     * @return
     */
    public WaBillAnalyzer getWaBillAnalyzer(String platform) {
        return Optional.ofNullable(getWaBillAnalyzerMap().getOrDefault(platform, null))
                .orElseThrow(() -> new RuntimeException("平台：" + platform + "未找到对对应的分析器"));
    }

    /**
     * 日账单统计
     *
     * @param waBillList
     * @return
     */
    public List<DailyBill> dailyBill(Map<LocalDate, DailyBill> dailyBillMap, List<WaBill> waBillList) {

        waBillList.stream()
                .forEach(waBill -> {
                    LocalDate date = waBill.getTradeTime().toLocalDate();
                    if (dailyBillMap.containsKey(date)) {
                        DailyBill dailyBill = dailyBillMap.get(date);
                        dailyBill.add(waBill.getInOrOut(), waBill.getAmt(), waBill.getPlatform());

                    }

                });

        return dailyBillMap.values().stream()
                .sorted(Comparator.comparing(DailyBill::getDate))
                .collect(Collectors.toList());
    }

    /**
     * 月账单统计
     *
     * @param monthBillMap
     * @param waBillList
     * @return
     */
    public List<MonthBill> monthBill(Map<YearMonth, MonthBill> monthBillMap, List<WaBill> waBillList) {
        waBillList.stream()
                .forEach(waBill -> {
                    YearMonth yearMonth = YearMonth.from(waBill.getTradeTime());
                    if (monthBillMap.containsKey(yearMonth)) {
                        MonthBill monthBill = monthBillMap.get(yearMonth);
                        monthBill.add(waBill.getInOrOut(), waBill.getAmt(), waBill.getPlatform());

                    }

                });

        return monthBillMap.values().stream()
                .sorted(Comparator.comparing(MonthBill::getMonth))
                .collect(Collectors.toList());
    }


}
